#coding=utf-8
#LightCam 期货行情生成程序
#init by hfh 20150812

import multiprocessing,subprocess,threading
import socket,os,time,re,traceback,sys
import lc_libs

if __name__ == "__main__":

	lchome=lc_libs.lc_getlchome()
	scname=sys.argv[0].split('.')[0]
	logfilename=lchome+'/log/'+scname+'.log'
	confname=lchome+'/conf/'+scname+'.conf'
	
	today=time.strftime('%Y%m%d',time.localtime())
	os.system("mv %s %s.%s" % (logfilename,logfilename,today))
	
	logfile=file(logfilename,'a')
	logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logfile.write(logdate+" Started.\n")
	logfile.flush()
	
	agent_hostname=lc_libs.lc_gethostname()

	while True:
		time.sleep(0.1)
	
		qhhq_current={}
		qhhq_today={}

		try:
			
			#初始化行情输入文件
			filename="/monitor/product/prognosis/server/configuration/Tradexml_10s/quotdata.csv"

			#行情数据处理
			for line in open(filename).readlines():
				lines=line.split(',')
				Instrument = lines[4].strip('"').strip("'")
				CurrentPrice = lines[8].strip('\r\n')
				LastClosePrice = lines[9].strip('\r\n')
				TradeVolume = lines[10].strip('\r\n')
				Position = lines[11].strip('\r\n')
				LastSettlementPrice = lines[12].strip('\r\n')
				Time = lines[5].split()[3].strip('\r\n')
				
				#剃除中午非交易时间
				thistime=int(Time.split(':')[0])*3600+int(Time.split(':')[1]*60)+int(Time.split(':')[2])
				if thistime > 11*3600+30*60 and thistime < 13*3600:
					continue
				
				#当前期货行情写入
				if Instrument in qhhq_current.keys():
					pass
				else:
					qhhq_current[Instrument]={}
				qhhq_current[Instrument]['CurrentPrice'] = CurrentPrice
				qhhq_current[Instrument]['LastClosePrice'] = LastClosePrice
				qhhq_current[Instrument]['TradeVolume'] = TradeVolume
				qhhq_current[Instrument]['Position'] = Position
				qhhq_current[Instrument]['LastSettlementPrice'] = LastSettlementPrice
				
				#当天期货行情写入
				if Instrument in qhhq_today.keys():
					pass
				else:
					qhhq_today[Instrument]={}
				if Time in qhhq_today[Instrument].keys():
					pass
				else:
					qhhq_today[Instrument][Time]={}
				qhhq_today[Instrument][Time]['CurrentPrice'] = CurrentPrice
			
			
			#输出期货行情XML文件
			ofile=open("/monitor/bin/app/TradeMon/QihuoHangqing.xml","w")

			#文件头
			ofile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
			ofile.write("<data>\n")
			
			#当前期货行情输出
			ofile.write("<variable name='CurrentQihuoHangqingData'>\n")
			ofile.write("<row>\n")
			ofile.write("<column>Instrument</column>\n")
			ofile.write("<column>CurrentPrice</column>\n")
			ofile.write("<column>LastClosePrice</column>\n")
			ofile.write("<column>TradeVolume</column>\n")
			ofile.write("<column>Position</column>\n")
			ofile.write("<column>LastSettlementPrice</column>\n")
			ofile.write("</row>\n")
			for Instrument in qhhq_current.keys():
				ofile.write("<row>\n")
				ofile.write("<column>%s</column>\n" % Instrument)
				ofile.write("<column>%s</column>\n" % qhhq_current[Instrument]['CurrentPrice'])
				ofile.write("<column>%s</column>\n" % qhhq_current[Instrument]['LastClosePrice'])
				ofile.write("<column>%s</column>\n" % qhhq_current[Instrument]['TradeVolume'])
				ofile.write("<column>%s</column>\n" % qhhq_current[Instrument]['Position'])
				ofile.write("<column>%s</column>\n" % qhhq_current[Instrument]['LastSettlementPrice'])
				ofile.write("</row>\n")
			ofile.write("</variable>\n")

			#当天期货行情输出
			ofile.write("<variable name='TodayQihuoHangqingData'>\n")
			ofile.write("<row>\n")
			ofile.write("<column>Instrument</column>\n")
			ofile.write("<column>Time</column>\n")
			ofile.write("<column>CurrentPrice</column>\n")
			ofile.write("</row>\n")
			for Instrument in qhhq_today.keys():
				#for Time in qhhq_today[Instrument].keys():
				list=sorted(qhhq_today[Instrument].iteritems(),key=lambda d:d[0],reverse=False)
				for item in list:
					ofile.write("<row>\n")
					ofile.write("<column>%s</column>\n" % Instrument)
					ofile.write("<column>%s</column>\n" % item[0])
					ofile.write("<column>%s</column>\n" % item[1]['CurrentPrice'])
					ofile.write("</row>\n")
			ofile.write("</variable>\n")
			
			ofile.write("</data>\n")
			ofile.close()
			
		except Exception:
			excinfo=traceback.format_exc()
			logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
			logfile.write(logdate+" :\n"+excinfo+"\n")
			logfile.flush()

		time.sleep(10)
